﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;

namespace ExamAnalysis.Manage
{
    public partial class ReasonInfo : System.Web.UI.Page
    {
        static int subjectid = 0;
        static int reasonid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var myReason = from r in db.ReasonInfo
                                   select new { ReasonID = r.ReasonID, ReasonName = r.ReasonName, ReasonCode = r.ReasonCode, ReasonDescribe = r.ReasonDescribe, SubjectID = r.SubjectID, SubjectName = r.SubjectInfo.SubjectName };
                    this.Store2.DataSource = myReason;
                    this.Store2.DataBind();
                }
            }
        }

        protected void Button_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                subjectid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var mySubject = from s in db.SubjectInfo
                              where s.SubjectID == subjectid
                              select s.SubjectName;
                foreach (var subject in mySubject)
                {
                    DropDownField1.Text = subject.ToString();
                }
                var myReason = from r in db.ReasonInfo
                              where r.SubjectID == subjectid
                               select new { ReasonID = r.ReasonID, ReasonName = r.ReasonName, ReasonCode = r.ReasonCode, ReasonDescribe = r.ReasonDescribe, SubjectID = r.SubjectID, SubjectName = r.SubjectInfo.SubjectName };
                this.Store2.DataSource = myReason;
                this.Store2.DataBind();
            }
            subjectid= 0;
        }

        protected void Button2_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            ReasonCode.Text = "";
            ReasonName.Text = "";
            ReasonDescribe.Text = "";
            DropDownField2.Text = "请选择";
            subjectid = 0;
            this.FormWindow.Visible = true;
            this.FormWindow.Show();
            Button6.Show();
            Button7.Hide();
        }

        protected void Button3_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    reasonid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newreason = from r in db.ReasonInfo
                                   where r.ReasonID == reasonid
                                   select r;
                    ExamAnalysis.Model.ReasonInfo newr = newreason.First();
                    subjectid = newr.SubjectID;
                    ReasonCode.Text = newr.ReasonCode;
                    ReasonName.Text = newr.ReasonName;
                    ReasonDescribe.Text = newr.ReasonDescribe;
                    DropDownField2.Text = newr.SubjectInfo.SubjectName;
                }
                this.FormWindow.Visible = true;
                this.FormWindow.Show();
                Button6.Hide();
                Button7.Show();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个错因").Show();
            }
        }

        protected void Button4_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.ClassInfoGridPanel.GetSelectionModel() as RowSelectionModel;
            if (sm.SelectedRows.Count != 0)
            {
                foreach (SelectedRow row in sm.SelectedRows)
                {
                    reasonid = Convert.ToInt32(row.RecordID);
                }
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    var newreason = from r in db.ReasonInfo
                                    where r.ReasonID == reasonid
                                    select r;
                    ExamAnalysis.Model.ReasonInfo newr = newreason.First();
                    db.ReasonInfo.DeleteOnSubmit(newr);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", "删除成功").Show();
                    Response.Redirect("ReasonInfo.aspx");
                }
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择一个错因").Show();
            }
        }

        protected void Button5_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
            foreach (SelectedRow row in sm.SelectedRows)
            {
                subjectid = Convert.ToInt32(row.RecordID);
            }
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var mySubject = from s in db.SubjectInfo
                                where s.SubjectID == subjectid
                                select s.SubjectName;
                foreach (var subject in mySubject)
                {
                    DropDownField2.Text = subject.ToString();
                }
            }
        }

        protected void Add_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            if (DropDownField2.Text.ToString().Equals("请选择"))
            {
                X.Msg.Alert("提示信息", String.Format("请选择一个科目")).Show();
            }
            else
            {
                ExamAnalysis.Model.ReasonInfo newr = new ExamAnalysis.Model.ReasonInfo();
                newr.ReasonCode = ReasonCode.Text;
                newr.ReasonName = ReasonName.Text;
                newr.ReasonDescribe = ReasonDescribe.Text;
                newr.SubjectID = subjectid;
                using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
                {
                    db.ReasonInfo.InsertOnSubmit(newr);
                    db.SubmitChanges();
                    X.Msg.Alert("提示信息", String.Format("成功添加班级")).Show();
                    subjectid = 0;
                }
                Response.Redirect("ReasonInfo.aspx");
            }
        }

        protected void Change_Click(object sender, Ext.Net.DirectEventArgs e)
        {
            using (ExamAnalysisDataContext db = new ExamAnalysisDataContext())
            {
                var newreason = from r in db.ReasonInfo
                                where r.ReasonID == reasonid
                                select r;
                ExamAnalysis.Model.ReasonInfo newr = newreason.First();
                newr.ReasonCode = ReasonCode.Text;
                newr.ReasonName = ReasonName.Text;
                newr.ReasonDescribe = ReasonDescribe.Text;
                newr.SubjectID = subjectid;
                db.SubmitChanges();
                X.Msg.Alert("提示信息", String.Format("成功修改错因信息")).Show();
                subjectid = 0;
                reasonid = 0;
            }
            Response.Redirect("ReasonInfo.aspx");
        }
    }
}